/**
 * Author: Sharath
 * Date: Jan 29, 2004
 * Time: 01:40:00
 */
package com.scalar.chiptrack.sales.dao;

import java.util.ArrayList;
import java.util.HashMap;

import com.scalar.ScalarException;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.sales.SalesOrderInvoice;
import com.scalar.chiptrack.sales.SalesOrderInvoiceLineItemList;
import com.scalar.chiptrack.sales.SalesOrderInvoiceList;
import com.scalar.chiptrack.sales.dao.local.LocalSalesOrderInvoiceDAOImpl;
import com.scalar.chiptrack.utils.ConfigManager;

/**
 *  Data access class for Sales Order Invoice.  The methods
 *  in this class internally call the respective methods on
 *  GPSalesOrderInvoiceDAOImpl or LocalSalesOrderInvoiceDAOImpl based on the
 *  integration type
 *
 *  @author Sharath
 *  @version
 */

public class SalesOrderInvoiceDAO implements Constants
{
    private static SalesOrderInvoiceDAO  sSalesOrderInvoiceDAO;
    private static ISalesOrderInvoiceDAO sISalesOrderInvoiceDAO;


    static
    {
        String finIntegrationType = ConfigManager.getFinanceIntegrationType();
        if ( ( finIntegrationType == null ) || ( finIntegrationType.equalsIgnoreCase( FINANCE_INTEGRATION_TYPE_LOCAL ) ) )
        {
            sISalesOrderInvoiceDAO = new LocalSalesOrderInvoiceDAOImpl();
        }

    }


    public synchronized static SalesOrderInvoiceDAO getInstance()
    {
        if ( sSalesOrderInvoiceDAO == null )
        {
            sSalesOrderInvoiceDAO = new SalesOrderInvoiceDAO();
        }
        return sSalesOrderInvoiceDAO;
    }

    /**
     * Singleton - Private constructor
     */
    private SalesOrderInvoiceDAO()
    {
    }

    /**
     * Saves Sales Order Invoice Information in the database
     *
     * @param salesOrderInvoice
     * @throws ScalarException
     */
    public static void saveSalesOrderInvoice( DBTransaction dbTransaction, SalesOrderInvoice salesOrderInvoice ) throws ScalarException
    {
        sISalesOrderInvoiceDAO.saveSalesOrderInvoice( dbTransaction, salesOrderInvoice );
    }

    /**
     * Updates Sales Order Invoice Information in the database
     *
     * @param salesOrderInvoice
     * @throws ScalarException
     */
    public static void updateSalesOrderInvoice( DBTransaction dbTransaction, SalesOrderInvoice salesOrderInvoice ) throws ScalarException
    {
        sISalesOrderInvoiceDAO.updateSalesOrderInvoice( dbTransaction, salesOrderInvoice );
    }

    /**
     * Displays all Sales Order Invoices
     *
     * @param salesOrderInvoice
     * @return SalesOrderInvoiceList
     * @throws ScalarException
     */

    public static SalesOrderInvoiceList loadSalesOrderInvoices( SalesOrderInvoice salesOrderInvoice ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadSalesOrderInvoices( salesOrderInvoice );
    }


    /**
     * Displays Receivings Transaction Information
     *
     * @param salesOrderInvoice
     * @return SalesOrderInvoice
     * @throws ScalarException
     */

    public static SalesOrderInvoice loadSalesOrderInvoice( SalesOrderInvoice salesOrderInvoice ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadSalesOrderInvoice( salesOrderInvoice );
    }

    /**
     * Displays all Sales Order Invoice Line Items
     *
     * @param  salesOrderInvoice
     * @return SalesOrderInvoiceList
     * @throws ScalarException
     */

    public static SalesOrderInvoiceLineItemList loadSalesOrderInvoiceLineItems( SalesOrderInvoice salesOrderInvoice ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadSalesOrderInvoiceLineItems( salesOrderInvoice );
    }
    /**
     * Loads the Receipt number for New Purchase Order
     *
     * @return String
     * @throws ScalarException
     */

    public static String loadInvoiceNumber() throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadInvoiceNumber();
    }

    /**
     * Returns if a Invoice Number exists
     *
     * @param salesOrderInvoice
     * @return boolean
     * @throws ScalarException
     */
    public static boolean salesOrderInvoiceNumberExists( SalesOrderInvoice salesOrderInvoice ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.salesOrderInvoiceNumberExists( salesOrderInvoice );
    }
    /**
     * Loads the Default Site for New Sales Invoice
     *
     * @return String
     * @throws ScalarException
     */

    public static String loadDefaultSite() throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadDefaultSite();
    }
    /**
     * Loads the Invoice Return number for New Sales Invoice
     *
     * @return String
     * @throws ScalarException
     */

    public static String loadInvoiceReturnNumber() throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadInvoiceReturnNumber();
    }

    /**
     * Returns List of Items with Item Price .
     * @return ArrayList
     * @throws ScalarException
     */
    public static HashMap loadMarketingPartNosUnitCost( ) throws ScalarException
    {
       return sISalesOrderInvoiceDAO.loadMarketingPartNosUnitCost();
    }
   /**
     * Returns List of Items with Item Description .
     * @return ArrayList
     * @throws ScalarException
     */
    public static HashMap loadMarketingDescriptions( ) throws ScalarException
    {
       return sISalesOrderInvoiceDAO.loadMarketingDescriptions();
    }

   /**
     * Returns List of Manufacturing Part Nos( Items )
     * @return ArrayList
     * @throws ScalarException
     */

    public static ArrayList loadAllManfacturingPartNumbers() throws ScalarException
   {
       return sISalesOrderInvoiceDAO.loadAllManfacturingPartNumbers();
   }
    /**
     * Returns List of Manufacturing Part Nos (Items) with Item Description .
     * @return HashMap
     * @throws ScalarException
     */
    public static HashMap loadAllManfacturingPartNumbersWithComments() throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadAllManfacturingPartNumbersWithComments();
    }
    /**
     * Returns List of Manufacturing Part Nos (Items) with Item Description .
     * @return HashMap
     * @throws ScalarException
     */
    public static HashMap loadAllManfacturingPartNumbersCost() throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadAllManfacturingPartNumbersCost();
    }
    /**
     * Returns List of Sales Order Numbers Based on Customer Id.
     * @return  ArrayList
     * @throws ScalarException
     */
    public static ArrayList loadSONumbersByCustomer( SalesOrderInvoice salesOrderInvoice ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadSONumbersByCustomer( salesOrderInvoice );
    }
    /**
    * Returns Empty List of Empy Sales Order Number List.
    * @return  ArrayList
    * @throws ScalarException
    */
    public static ArrayList loadEmptySalesOrdersList() throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadEmptySalesOrdersList();
    }
    /**
     * Displays all Sales Orders Line Items for Invoice
     *
     * @param  selectedSONumber
     * @return SalesOrderInvoiceList
     * @throws ScalarException
     */

    public static SalesOrderInvoiceLineItemList loadSalesOrdersInvoiceLineItemsBySO( String selectedSONumber, int invoiceCount ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadSalesOrdersInvoiceLineItemsBySO( selectedSONumber, invoiceCount );
    }

    /**
     * Loads the line item list for which sales order invoice is to be created from the packing slip data
     * This method accepts packingShipLabel and salesOrderNumber as the search criteria
     *
     * @param packingSlipLabel
     * @param salesOrderNumber
     * @return SalesOrderInvoiceList
     * @throws ScalarException
     */
    public static SalesOrderInvoiceLineItemList loadSalesOrdersLineItemFromPackingSlipData( String packingSlipLabel, String salesOrderNumber, String year, String customerPO, int customerId, int locationId ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadSalesOrdersLineItemFromPackingSlipData( packingSlipLabel, salesOrderNumber, year, customerPO, customerId, locationId );
    }

    /**
     * This will update invoice generated field in the packing slip line item table for those invoices whose invoice has been generated
     *
     * @param dbTransaction
     * @param salesOrderInvoiceLineItemList
     * @throws ScalarException
     */
    public static void updatePackingSlipLineItemInformation( DBTransaction dbTransaction, SalesOrderInvoiceLineItemList salesOrderInvoiceLineItemList ) throws ScalarException
    {
        sISalesOrderInvoiceDAO.updatePackingSlipLineItemInformation( dbTransaction,salesOrderInvoiceLineItemList );
    }

    /**
     * This method checks if the given invoice number is existing in the database
     *
     * @param dbTransaction
     * @param invoiceNumber
     * @param docType
     * @throws ScalarException
     */
    public static boolean checkInvoiceNumberExists( DBTransaction dbTransaction, String invoiceNumber, int docType ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.checkInvoiceNumberExists( dbTransaction, invoiceNumber, docType );
    }

    /**
     * This method sets the lot details from which the quantities have been pulled for the respective line items
     *
     * @param dbTransaction
     * @param salesOrderInvoiceLineItemList
     * @throws ScalarException
     */
    public static void setSalesOrderInvoiceLineItemLotDetails( DBTransaction dbTransaction, SalesOrderInvoiceLineItemList salesOrderInvoiceLineItemList ) throws ScalarException
    {
        sISalesOrderInvoiceDAO.setSalesOrderInvoiceLineItemLotDetails( dbTransaction, salesOrderInvoiceLineItemList );
    }

    /**
     * This method saves the lot details from which the quantities have been pulled for the respective line items
     *
     * @param dbTransaction
     * @param salesOrderInvoice
     * @return int value giving the status of the save
     *          <li> 0 : Saved successfully
     *          <li> 1 : Sales Invoice line item is not present
     *          <li> 2 : Lot is not existing
     *          <li> 3 : Insufficient qty
     *
     * @throws ScalarException
     */
    public static int saveSalesOrderInvoiceLineItemLotDetails( DBTransaction dbTransaction, SalesOrderInvoice salesOrderInvoice ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.saveSalesOrderInvoiceLineItemLotDetails( dbTransaction, salesOrderInvoice);
    }

    /**
     * This method sets the gp location of the sales order invoice line items in the list
     *
     * @param dbTransaction
     * @param salesOrderInvoiceLineItemList
     * @throws ScalarException
     */
    public static void setSalesOrderInvoiceLineItemGpLocation( DBTransaction dbTransaction, SalesOrderInvoiceLineItemList salesOrderInvoiceLineItemList ) throws ScalarException
    {
        sISalesOrderInvoiceDAO.setSalesOrderInvoiceLineItemGpLocation( dbTransaction, salesOrderInvoiceLineItemList );
    }

    /**
     * This will update invoice Number field in the Customer Shipping table for those Packing Slip whose invoice has been generated
     *
     * @param dbTransaction
     * @param salesOrderInvoice
     * @throws ScalarException
     */
    public static void updateInvoiceNumber( DBTransaction dbTransaction, SalesOrderInvoice salesOrderInvoice ) throws ScalarException
    {
        sISalesOrderInvoiceDAO.updateInvoiceNumber( dbTransaction,salesOrderInvoice );
    }

    /**
     * This methot is to load all customer name
     * @param dbTransaction
     * @return ArrayList
     * @throws ScalarException
     */
    public static ArrayList loadAllCustomers(DBTransaction dbTransaction) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadAllCustomers(dbTransaction);
    }

    /**
     * This methot is to load all Location Names
     * @param dbTransaction
     * @return ArrayList
     * @throws ScalarException
     */
    public static ArrayList loadAllLocations(DBTransaction dbTransaction ) throws ScalarException
    {
        return sISalesOrderInvoiceDAO.loadAllLocations(dbTransaction);
    }


    /**
     * This Method is to delete Sales order Invoice
     * @param dbTransaction
     * @param salesOrderInvoice
     * @throws ScalarException
     */
    public static void deleteSalesOrderInvoice( DBTransaction dbTransaction , SalesOrderInvoice salesOrderInvoice )  throws ScalarException
    {
         sISalesOrderInvoiceDAO.deleteSalesOrderInvoice( dbTransaction, salesOrderInvoice );
    }

    /**
     * This Method is to update Sales order Invoice details in CT
     * @param dbTransaction
     * @param salesOrderInvoice
     * @throws ScalarException
     */


    public static void updateInvoiceInfo( DBTransaction dbTransaction , SalesOrderInvoice salesOrderInvoice )  throws ScalarException
    {
        sISalesOrderInvoiceDAO.updateInvoiceInfo( dbTransaction, salesOrderInvoice );
    }



}